package com.bdqn.t330.ch11.servcie;

import com.bdqn.t330.ch11.entity.Right;
import com.bdqn.t330.ch11.entity.Role;
import com.bdqn.t330.ch11.entity.User;
import com.bdqn.t330.ch11.repository.RightRepository;
import com.bdqn.t330.ch11.repository.RoleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImp implements RoleService {

    @Resource
    RoleRepository roleRepository;

    @Resource
    RightRepository rightRepository;

    //@Cacheable(value = "role", keyGenerator = "keyGenerator")
    //@CachePut(value = "role", keyGenerator = "keyGenerator")
    @Override
    public List<Role> getRoleList() {
        Sort sort = new Sort(Sort.Direction.ASC, "roleId");
        return roleRepository.findAll(sort);
    }

    @Override
    public List<Right> findAllRights() {
        return rightRepository.findAll();
    }

    @Override
    public List<Right> findRightsByRole(Role role) {
        return rightRepository.findRightsByRolesOrderByRightCode(role);
    }

    @Override
    public Page<Role> getListService(Map<String, Object> param, Pageable pageable) {
        return roleRepository.findAll(new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if (param.get("formDate") != null) { //动态 SQL 开始时间
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("creationDate"), (Date) param.get("formDate")));
                }

                if (param.get("endDate") != null) { //动态 SQL 开始时间
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("creationDate"), (Date) param.get("endDate")));
                }

                /*if (param.get("usrName") != null && param.get("usrName").toString().length() > 0) { //动态 SQL
                    predicates.add(criteriaBuilder.like(root.get("usrName"), "%" + param.get("usrName") + "%"));
                }
                if (!param.get("roleId").equals("0") && Long.valueOf(param.get("roleId").toString()) > 0) { //动态 SQL
                    //predicates.add(criteriaBuilder.equal(root.get("roleId"), param.get("roleId")));
                    Role role = new Role();
                    role.setRoleId(Long.valueOf(param.get("roleId").toString()));
                    predicates.add(criteriaBuilder.equal(root.get("role"), role));
                }*/
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, pageable);
    }
}
